<extend name="Public/base" />

<block name="body">
    <!-- 标题栏 -->
    <div class="main-title">
        <h2>{$meta_title}</h2>
    </div>
    <div class="cf">
        <div class="fl">
            <a class="btn" href="{:U('addExercises')}">新 增</a>
        </div>

        <!-- 高级搜索 -->
        <div class="search-form fr cf">
            <div class="sleft">
                <select name="sid" class="subject">
                    <option value="0">请选择学科</option>
                    <volist name="subject_list" id="vo">
                        <option value="{$vo.id}" <if condition="$vo['id'] eq I('sid')">selected</if>>{$vo.name}</option>
                    </volist>
                </select>
                <select name="one_chapter_id" class="one_chapter">
                    <option value="0">请选择章节</option>
                </select>
                <select name="two_chapter_id" class="two_chapter">
                    <option value="0">请选择知识点</option>
                </select>

                <select name="style">
                    <option value="0">请选择类型</option>
                    <option value="1" <if condition="I('style') == 1">selected</if>>单选</option>
                    <option value="2" <if condition="I('style') == 2">selected</if>>多选</option>
                </select>
            </div>
            <a class="sch-btn" href="javascript:;" id="search" url="{:U('exercisesList')}"><i class="btn-search"></i></a>
        </div>

    </div>
    <!-- 数据列表 -->
    <div class="data-table table-striped">
        <table>
            <thead>
            <tr>
                <th>ID</th>
                <th>学科</th>
                <th>章节 - 知识点</th>
                <th>题目</th>
                <th>解析</th>
                <th>类型</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <notempty name="_list">
                <volist name="_list" id="vo">
                    <tr>

                        <td width="5%">{$vo.id}</td>
                        <td width="10%">{$vo.sid|getSubjectName}</td>
                        <td width="15%">{$vo.one_chapter_id|getChapterName} - {$vo.two_chapter_id|getChapterName}</td>
                        <td width="25%">{$vo.question}</td>
                        <td width="20%">{$vo.analysis}</td>
                        <td width="5%"><if condition="$vo['style'] == 1">单选<else />多选</if></td>
                        <td width="10%">{$vo.create_time|time_format}</td>
                        <td width="10%">
                            <a href="{:U('upExercises',array('id'=>$vo['id']))}" class="authorize">编辑</a> |
                            <a href="{:U('delExercises',array('id'=>$vo['id']))}" class="authorize">删除</a>
                        </td>
                    </tr>
                </volist>
                <else/>
                <td colspan="8" class="text-center"> aOh! 暂时还没有内容! </td>
            </notempty>
            </tbody>
        </table>
    </div>
    <div class="page">
        {$_page}
    </div>
</block>

<block name="script">
    <script src="__STATIC__/thinkbox/jquery.thinkbox.js"></script>
    <script type="text/javascript">
        $(function (){
            var subject_list = {$subject_list_json};
            var sid = "{:I('sid')}";
            var one_chapter_id = "{:I('one_chapter_id')}";
            var two_chapter_id = "{:I('two_chapter_id')}";

            if(subject_list.length > 0){
                var option = "<option value='0'>请选择章节</option>";
                var two_option = "<option value='0'>请选择知识点</option>";
                //渲染学科下拉列表
                for(var i = 0; i < subject_list.length; i++){
                    if(subject_list[i].id == sid){
                        chapter_list = subject_list[i].chapter_list;
                        //渲染章节下拉列表
                        for (var j = 0; j < subject_list[i].chapter_list.length; j++) {
                            var obj = subject_list[i].chapter_list[j];
                            var one_selected = '';
                            if(one_chapter_id == obj.id) one_selected = 'selected';
                            option += "<option value='"+ obj.id +"' "+ one_selected +">"+ obj.name +"</option>";

                            if(one_chapter_id == obj.id){
                                var two_chapter_list = obj.son_list;
                                if(two_chapter_list.length > 0){
                                    //渲染知识点下拉列表
                                    for (var k = 0; k < two_chapter_list.length; k++) {
                                        var two_obj = two_chapter_list[k];
                                        var two_selected = '';
                                        if(two_chapter_id == two_obj.id){
                                            two_selected = 'selected';
                                        }
                                        two_option += "<option value='"+ two_obj.id +"' "+ two_selected +">"+ two_obj.name +"</option>";
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
                $('.one_chapter').html(option);
                $('.two_chapter').html(two_option)
            }
        })

        $('.subject').change(function (){
            var subject_list = {$subject_list_json};
            var sid = $(this).val();

            if(subject_list.length > 0){
                var option = "<option value='0'>请选择章节</option>";
                for(var i = 0; i < subject_list.length; i++){
                    if(subject_list[i].id == sid){
                        chapter_list = subject_list[i].chapter_list;
                        for (var j = 0; j < subject_list[i].chapter_list.length; j++) {
                            var obj = subject_list[i].chapter_list[j];
                            option += "<option value='"+ obj.id +"'>"+ obj.name +"</option>";
                        }
                        break;
                    }
                }
                $('.one_chapter').html(option);
                $('.two_chapter').html('<option value="0">请选择知识点</option>')
            }
        })

        var chapter_list = [];
        $('.one_chapter').change(function (){
            var one_chapter_id = $(this).val();
            if(chapter_list.length){
                var option = "<option value='0'>请选择知识点</option>";
                for(var i = 0; i < chapter_list.length; i++){
                    if(chapter_list[i].id == one_chapter_id){
                        for (var j = 0; j < chapter_list[i].son_list.length; j++) {
                            var obj = chapter_list[i].son_list[j];
                            option += "<option value='"+ obj.id +"'>"+ obj.name +"</option>";
                        }
                        break;
                    }
                }
                $('.two_chapter').html(option);
            }
        })

        //搜索功能
        $('#search').click(function(){
            var url = $(this).attr('url').replace('.html', '');
            var query  = $('.search-form').find('input').serialize();
            var squery = $('.search-form').find('select').serialize();
            query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
            query = query.replace(/^&/g,'');
            squery=squery.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
            squery=squery.replace(/^&/g,'');
            if( url.indexOf('?')>0 ){
                url += '&' + query+'&'+squery;
            }else{
                url += '?' + query+'&'+squery;
            }
            window.location.href = url;
        });


        //导航高亮
        highlight_subnav('{:U("Manage/exercisesList")}');
    </script>
</block>